package cn.tedu.person.web;

import cn.tedu.person.ex.ServiceException;
import io.swagger.models.auth.In;
import lombok.Data;

import java.io.Serializable;

@Data
public class JsonResult implements Serializable {
    //操作的状态码
    private Integer state;
    //操作失败的时候描述的文本
    private String message;
    //操作成功时响应的数据
    private Object data;

    //成功时不响应数据
    public static JsonResult ok(){
        return ok(null);
    }
    //成功时  需要响应的数据
    public static JsonResult ok(Object object){
        JsonResult jsonResult=new JsonResult();
        jsonResult.setState(ServiceCode.OK.getValue());
        jsonResult.setData(object);
        return jsonResult;
    }

    public static JsonResult fail(ServiceException e){
        return fail(e.getServiceCode(),e.getMessage());
    }
    //失败时相应给客户端的
    public static JsonResult fail(ServiceCode serviceCode,String message){
        JsonResult jsonResult=new JsonResult();
        jsonResult.setState(serviceCode.getValue());
        jsonResult.setMessage(message);
        return jsonResult;
    }
}
